﻿
namespace SampleCode.VirtualFS.ViewModel
{
    /// <summary>
    /// Some extension methods for VirtualFS object model
    /// </summary>
    public static class FsExtensionMethods
    {
        /// <summary>
        /// Compares file full paths
        /// </summary>
        /// <param name="thisFile">this file</param>
        /// <param name="thatFile">that file</param>
        /// <returns>true when equal</returns>
        public static bool IsTheSameFile(this FsFileInfo thisFile, FsFileInfo thatFile)
        {
            return thisFile.FullName == thatFile.FullName;
        }

        /// <summary>
        /// Compares Folder full paths
        /// </summary>
        /// <param name="thisFolder">this Folder</param>
        /// <param name="thatFolder">that Folder</param>
        /// <returns>true when equal</returns>
        public static bool IsTheSameFolder(this FsFolderInfo thisFolder, FsFolderInfo thatFolder)
        {
            return thisFolder.FullName == thatFolder.FullName;
        }

        /// <summary>
        /// Retuns a full path to the folder (excluding folder's Name)
        /// </summary>
        /// <param name="folder">folder</param>
        /// <returns>A full path to the folder (excluding folder's Name)</returns>
        public static string Path(this FsFolderInfo folder)
        {
            return folder.FullName.Substring(0, folder.FullName.Length - folder.Name.Length - 1);
        }

        /// <summary>
        /// Retuns a full path to the File (excluding File's Name)
        /// </summary>
        /// <param name="file">File</param>
        /// <returns>A full path to the File (excluding File's Name)</returns>
        public static string Path(this FsFileInfo file)
        {
            return file.FullName.Substring(0, file.FullName.Length - file.Name.Length - 1);
        }

        /// <summary>
        /// Checks if a file is inside a folder
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="file"></param>
        /// <returns>true if a file is inside a folder</returns>
        public static bool ContainsFile(this FsFolderInfo folder, FsFileInfo file)
        {
            return folder.FullName == file.Path();
        }

        /// <summary>
        /// Checks if a file is inside a folder
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="fileName"></param>
        /// <returns>true if a file is inside a folder</returns>
        public static bool ContainsFile(this FsFolderInfo folder, string fileName)
        {
            int pos = fileName.LastIndexOf(FsDisk.ExternalPathSeparator);
            if (pos < 0)
                return false;

            return folder.FullName == fileName.Substring(0, pos);
        }

        /// <summary>
        /// Checks if a subfolder is a direct child of a folder
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="subFolder"></param>
        /// <returns>true if a subfolder is a direct child of a folder</returns>
        public static bool ContainsSubfolder(this FsFolderInfo folder, FsFolderInfo subFolder)
        {
            return folder.FullName == subFolder.Path();
        }

        /// <summary>
        /// Checks if a subfolder is a direct child of a folder
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="subfolderName"></param>
        /// <returns>true if a subfolder is a direct child of a folder</returns>
        public static bool ContainsSubfolder(this FsFolderInfo folder, string subfolderName)
        {
            int pos = subfolderName.LastIndexOf(FsDisk.ExternalPathSeparator);
            if (pos < 0)
                return false;

            return folder.FullName == subfolderName.Substring(0, pos);
        }
    }
}
